/**
 * There are <a href="https://github.com/thinkgem/jeesite">JeeSite</a> code generation
 */
package com.chlitina.store.modules.expert.service;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.chlitina.store.common.persistence.Page;
import com.chlitina.store.common.service.BaseService;
import com.chlitina.store.modules.expert.dao.ParameterCategoryDao;
import com.chlitina.store.modules.expert.entity.ParameterCategory;

/**
 * 参数类型Service
 * @author lvbo
 * @version 2013-06-04
 */
@Component
@Transactional(readOnly = true)
public class ParameterCategoryService extends BaseService {

	@SuppressWarnings("unused")
	private static Logger logger = LoggerFactory.getLogger(ParameterCategoryService.class);
	
	@Autowired
	private ParameterCategoryDao parameterCategoryDao;
	
	public ParameterCategory get(Long id) {
		return parameterCategoryDao.findOne(id);
	}
	
	public Page<ParameterCategory> find(Page<ParameterCategory> page, String searchName) {
		DetachedCriteria dc = parameterCategoryDao.createDetachedCriteria();
		if (StringUtils.isNotEmpty(searchName)){
			dc.add(Restrictions.like("name", "%"+searchName+"%"));
		}
		dc.addOrder(Order.asc("id"));
		return parameterCategoryDao.find(page, dc);
	}
	
	@Transactional(readOnly = false)
	public void save(ParameterCategory parameterCategory) {
		parameterCategoryDao.save(parameterCategory);
	}
	
	@Transactional(readOnly = false)
	public void delete(Long id) {
		parameterCategoryDao.delete(id);
	}
	
	@Transactional(readOnly = false)
	public List<ParameterCategory> findByName(String name) {
		DetachedCriteria dc = parameterCategoryDao.createDetachedCriteria();
		if (StringUtils.isNotEmpty(name)){
			dc.add(Restrictions.like("name", "%"+name+"%"));
		}
		dc.addOrder(Order.asc("id"));
		return (List<ParameterCategory>) parameterCategoryDao.find(dc);
	}
	
	@Transactional(readOnly = false)
	public List<ParameterCategory> findAll() {
		return (List<ParameterCategory>) parameterCategoryDao.findAll();
	}
	
	//获取当前记录排第几位
	public int getIndexById(Long currentId) {
		DetachedCriteria dc = parameterCategoryDao.createDetachedCriteria();
		dc.add(Restrictions.le("id", currentId));
		dc.addOrder(Order.asc("id"));
		return (int)parameterCategoryDao.count(dc);
	}
}
